<template>
  <div class="login-container">
      <div class="login-box">
          <el-form ref='loginformRef' :model='loginForm' :rules='loginFormRules'>
              <img src="./logo_index.png" alt="" style="width:65%">
              <el-form-item prop="mobile" >

                  <el-input placeholder="电话" v-model="loginForm.mobile" prefix-icon="el-icon-phone" ></el-input>
              </el-form-item>
               <el-form-item prop="code">
                  <el-input placeholder="验证码" v-model="loginForm.code" prefix-icon="el-icon-s-comment"></el-input>
              </el-form-item>
              <el-form-item style="text-align:left" prop="xieyi">
                 <el-checkbox v-model="loginForm.xieyi"></el-checkbox>
                 <span>我已近阅读并同意协议</span>
              </el-form-item>
              <el-form-item>
                  <el-button type="primary" style="width:100%" @click='login()' :loading='flag' :disabled="flag">登录</el-button>
              </el-form-item>
          </el-form>
      </div>
  </div>
</template>

<script>
import '@/assets/js/gt.js'
export default {
  name: 'LoginList',
  data () {
    var xieyiTest = function (rule, value, callback) {
      value ? callback() : callback(new Error('无条件遵循我们的协议'))
    }
    return {
      ctaobj: null,
      flag: false,
      loginForm: {
        mobile: '15110536104',
        code: '246810',
        xieyi: true
      },
      loginFormRules: {
        mobile: [{
          required: true,
          message: '请填写电话号码'
        }],
        code: [{
          required: true,
          message: '请填写验证码'
        }],
        xieyi: [{
          validator: xieyiTest
        }]

      }
    }
  },
  methods: {
    login () {
      this.$refs.loginformRef.validate(valid => {
        if (valid) {
          if (this.ctaobj) {
            return this.ctaobj.verify()
          }
          this.flag = true
          let pro = this.$http.get(`/captchas/${this.loginForm.mobile}`)
          pro.then((result) => {
            let { data } = result.data
            window.initGeetest({
            // 以下配置参数来自服务端 SDK
              product: 'bind',
              gt: data.gt,
              challenge: data.challenge,
              offline: !data.success,
              new_captcha: true
            }, (captchaObj) => {
              captchaObj.onReady(() => {
                this.ctaobj = captchaObj
                captchaObj.verify()
                // 验证码ready之后才能调用verify方法显示验证码
                this.flag = false
              }).onSuccess(() => {
                // your code
                this.loginAct()
              }).onError(function () {
                // your code
              })
              // 按钮提交事件
            })
          }).catch(
            () => {
              this.$message.error('出错了')
            })
        }
      })
    },
    loginAct () {
      var pro = this.$http.post('/authorizations', this.loginForm)
      pro
        .then(result => {
          if (result.data.message === 'OK') {
            window.sessionStorage.setItem('userinfo', JSON.stringify(result.data.data))
            this.$router.push({ name: 'home' })
          }
        })
        .catch(err => {
          return this.$message.error('用户名或密码错误' + err)
        })
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
    height: 100%;
    background-color:#ccc;
    display: flex;
    justify-content: center;
    align-items:center;
    background-image: url('./login_bg.jpg');
    background-size: cover;
    .login-box{
        width: 400px;
        height: 340px;
        background-color:white;
        display: flex;
        align-items:center;
        justify-content: center;
        .el-form{
            width: 75%;
            text-align: center;
            img{
                margin-bottom: 20px;
            }
        }
    }
}
</style>
